--测试环境
-- package.path = package.path .. ";?.lua"
--生产环境
package.path = "./plugin/EPM330/utils.lua" .. ";..\\?.lua"
package.path = "./plugin/EPM330/json.lua" .. ";..\\?.lua"

function HexToFloat( hexString )
	if hexString == nil then
		return 0
	end
	local t = type( hexString )
	if t == "string" then
		hexString = tonumber(hexString , 16)
	end
 
	local hexNums = hexString
 
	local sign = math.modf(hexNums/(2^31))
 
	local exponent = hexNums % (2^31)
	exponent = math.modf(exponent/(2^23)) -127
 
	local mantissa = hexNums % (2^23)
 
	for i=1,23 do
		mantissa = mantissa / 2
	end
	mantissa = 1+mantissa
--	print(mantissa)
	local result = (-1)^sign * mantissa * 2^exponent
	result = tonumber(string.format('%.3f', result))
	return result
end

require("utils")
require("json")

local pt

function TestEnv() --true:测试环境  false:生产环境
    if (string.find(package.path, "utils") ~= nil)
    then
        return false
    else
        return true
    end
end

if (TestEnv() == true)
then
    rxBuf = { 0x01, 0x03, 0x0F, 0x01, 0x94, 0xB8, 0x7B } --01 03 02 01 94 B8 7B
else
    rxBuf = {}
end

function Luaprint(str) --只在测试环境下的print
    if (TestEnv() == true)
    then
        print(str)
    end
end

-- Luaprint(package.path)
regAddr = 0x0000

local ptvlaue = 0
local ctvlaue = 0
local dptvlaue = 0
local dctvlaue = 0
local dpqvlaue = 0
local pqvlaue = 0

function GenerateGetRealVariables(sAddr, step)
    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }

    if (step == 0)
    then
        requestADU[1] = sAddr --通信地址
        requestADU[2] = 0x03 --功能码
        requestADU[3] = 0x40 --寄存器起始地址·  
        requestADU[4] = 0xff
        requestADU[5] = 0x00 --寄存器长度
        requestADU[6] = 0x30
        local VariableMapTemp = {
            Variable = requestADU
        }

        crc = GetCRCModbus(VariableMapTemp)
        if (crc == nil) then
            return VariableMap
        end

        -- crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        VariableMap = {
            Status = "0",
            Variable = requestADU
        }

        sAddr = 0x0000
        regAddr = 0x40ff

    end

    local tmp = ""
    for i = 1, #requestADU do
        tmp = tmp .. " " .. string.format("%02X", requestADU[i])
    end

    return VariableMap
end

function DeviceCustomCmd(sAddr, cmdName, cmdParam, step)

    local crc
    local requestADU = {}
    local VariableMap = {
        Status = "0",
        Variable = {},
    }
    if (step == 0)
    then
        requestADU[1] = sAddr --通信地址
        requestADU[2] = 0x03 --功能码
        requestADU[3] = 0x40 --寄存器起始地址·  
        requestADU[4] = 0xff
        requestADU[5] = 0x00 --寄存器长度
        requestADU[6] = 0x30
        local VariableMapTemp = {
            Variable = requestADU
        }

        crc = GetCRCModbus(VariableMapTemp)
        if (crc == nil) then
            return VariableMap
        end

        -- crc = GetCRCModbus(VariableMapTemp)
        requestADU[7] = math.modf(crc % 256)
        requestADU[8] = math.modf(crc / 256)

        VariableMap = {
            Status = "0",
            Variable = requestADU
        }

        sAddr = 0x0000
        regAddr = 0x40ff

    end

    local tmp = ""
    for i = 1, #requestADU do
        tmp = tmp .. " " .. string.format("%02X", requestADU[i])
    end

    return VariableMap
end

function AnalysisRx(sAddr, rxBufCnt)
    local mAddr = 0 --地址
    local mFunCode = 0 --功能码
    local mRegByte = 0 --有效字节数
    local variableMap = {
        Status = "1",
        Variable = {},
    }
    --字节数不足5位直接返回错误码
    if (rxBufCnt < 5) then
        rxBuf = {}
        return variableMap
    end
    for index = 1, rxBufCnt, 1 do
        mAddr = tonumber(rxBuf[index])
        mFunCode = rxBuf[index + 1]
        sAddr = tonumber(sAddr)
        if (mAddr == sAddr) then
            if (mFunCode == 0x03) then
                mRegByte = rxBuf[index + 2] --有效字节数
                if (rxBufCnt - 4 - index - mRegByte < 0) then
                    print("数据长度不够，没有数据域")
                    variableMap = {
                        Status = "1",
                        Variable = {}
                    }
                    rxBuf = {}
                    return variableMap
                end
                --取出数据域数据
                local pdu = {}
                for i = index, index + 2 + mRegByte do
                    if rxBuf[i] ~= nil then
                        table.insert(pdu, rxBuf[i])
                    end
                end
                local VariableMapTemp = {
                    Variable = pdu
                }
                local variable = {}
                local variableIndex = 1
                local dataIndex = index + 3
                if (regAddr == 0x40ff) then
                    local valuept = (rxBuf[dataIndex] * 256 + rxBuf[dataIndex + 1])
                    pt = valuept
                    variable[variableIndex] = utils.AppendVariable(variableIndex, "PT", valuept, valuept)
                        variableIndex = variableIndex + 1

                    local params1 = {"Ua","Ub","Uc","Ul","Uab","Ubc","Uca","Un"}
                    for pIndex = 1, #params1, 1 do
                        local Value1 = (rxBuf[dataIndex + 2 + (pIndex - 1) * 2] * 256 + rxBuf[dataIndex + 3 + (pIndex - 1) * 2])
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params1[pIndex], Value1*pt, Value1)
                        variableIndex = variableIndex + 1
                    end

                    local params2 = {"Ia","Ib","Ic","I"}
                    for pIndex = 1, #params2, 1 do
                        local Value2 = (rxBuf[dataIndex + 18 + (pIndex - 1) * 2] * 256 + rxBuf[dataIndex + 19 + (pIndex - 1) * 2])
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params2[pIndex], Value2, Value2)
                        variableIndex = variableIndex + 1
                    end

                    local valueF = (rxBuf[dataIndex + 26] * 256 + rxBuf[dataIndex + 27])
                    variable[variableIndex] = utils.AppendVariable(variableIndex, "FR", valueF, valueF)
                        variableIndex = variableIndex + 1

                    local params3 = {"Pa","Pb","Pc","P","Qa","Qb","Qc","Q","Sa","Sb","Sc","S",}
                    for pIndex = 1, #params3, 1 do
                        local Value3 = (rxBuf[dataIndex + 28 + (pIndex - 1) * 2] * 256 + rxBuf[dataIndex + 29 + (pIndex - 1) * 2])
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params3[pIndex], Value3, Value3)
                        variableIndex = variableIndex + 1
                    end

                    local params4 = {"Pfa","Pfb","Pfc","Pf"}
                    for pIndex = 1, #params4, 1 do
                        local Value4 = (rxBuf[dataIndex + 52 + (pIndex - 1) * 2] * 256 + rxBuf[dataIndex + 53 + (pIndex - 1) * 2])
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params4[pIndex], Value4, Value4)
                        variableIndex = variableIndex + 1
                    end

                    local params5 = {"TotalEnergy","Epout","Eqin","Eqout"}
                    for pIndex = 1, #params5, 1 do
                        local Value5 = (rxBuf[dataIndex + 80 + (pIndex - 1) * 4] * 256 + rxBuf[dataIndex + 81 + (pIndex - 1) * 4]) * 65536 +
                        rxBuf[dataIndex + 82 + (pIndex - 1) * 4] * 256 + rxBuf[dataIndex + 83 + (pIndex - 1) * 4]
                        variable[variableIndex] = utils.AppendVariable(variableIndex, params5[pIndex], Value5, Value5)
                        variableIndex = variableIndex + 1
                    end

                    variableMap = {
                        Status = "0",
                        Variable = variable
                    }
                    rxBuf = {}
                    return variableMap
                end

            elseif (mFunCode == 0x85) then
                print("0x85发文发送异常！")
                variableMap = {
                    Status = "1",
                    Variable = {}
                }
                rxBuf = {}
                return variableMap
            end
        end
    end
    rxBuf = {}
    return variableMap
end


-- if (TestEnv() == true)
-- then
--     GenerateGetRealVariables(1, 0)
--     AnalysisRx(1, 7)
-- end
